<template>
  <div>
    <!-- 搜索框 -->
    <TopFilter :list="topFilterList" :cols="3" :collapse="false" @filterChange="changeHandle" @onCollapse="collapseHandle"></TopFilter>

    <!-- 答案信息 -->
    <FilterTable
      ref="chooseAnswerGroopTable"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'rows'"
      :height="'200px'"
      :selectionType="'single'"
      columnsRef="wjdyXzdaDaxxTable"
      :onPageChange="clearAnswerTable"
      :onRowSelectChange="arr => (this.chooseAnswer = arr)"
      :onColumnsChange="columns => (this.columns = columns)"
    >
    </FilterTable>

    <!-- 选项信息 -->
    <FilterTable
      ref="chooseAnswerTable"
      :columns="answerTableColums"
      :fetchapi="getAnswerApi"
      :params="fetchAnswerParams"
      :datakey="'rows'"
      :height="'200px'"
      :isSelectColumn="false"
      :isToperInfo="false"
      :isColumnFilter="false"
      columnsRef="wjdyXzdaXxxxTable"
      :onSyncTableData="tableChangeFun"
      :onColumnsChange="columns => (this.answerTableColums = columns)"
    >
    </FilterTable>
    <div style="text-align:center">
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="req">确 定</el-button>
        <el-button type="primary" @click="goback">取 消</el-button>
      </span>
    </div>
  </div>
</template>
<script>
import { mapState } from 'vuex';
import { dictionary } from '@/utils/dictMixin';
import { getAnswerInfo, delAnswerGroupAndAnswerById, queryAnswersPagByIds, getAnswerStringById } from '@/api/customerRepair/visitRules/questionnaireMake2';

export default {
  name: 'AnswerGroupSelect',
  components: {},
  mixins: [dictionary],
  data() {
    this.BaseTable = null;
    this.AnswerTable = null;
    return {
      topFilterList: this.createTopFilterList(),
      fetchApi: getAnswerInfo, // 获取答案表格接口
      fetchParams: { xhrAbort: true }, // 获取答案表格参数
      columns: this.createTableColumns(), // 答案信息表格列
      getAnswerApi: queryAnswersPagByIds, // 获取选项表格接口
      fetchAnswerParams: { xhrAbort: true }, // 获取选项表格参数
      answerTableColums: this.createAnswerTableColums(), // 选项信息表格列
      chooseAnswer: [], // 选择的答案
      tableData: []
    };
  },
  mounted() {
    this.BaseTable = this.$refs.chooseAnswerGroopTable.$refs.pageTable;
    this.AnswerTable = this.$refs.chooseAnswerTable.$refs.pageTable;
  },
  methods: {
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          fixed: 'left',
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 80,
          fixed: 'left',
          render: props => {
            return (
              <div>
                <el-button size="mini" type="text" onClick={() => this.getAnswerByGroupId(props.row)}>
                  选项信息
                </el-button>
              </div>
            );
          }
        },
        {
          title: '答案组ID',
          dataIndex: 'ANSWER_GROUP_ID',
          filterType: 'input'
        },
        {
          title: '答案名称',
          dataIndex: 'ANSWER_GROUP_NAME',
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '答案说明',
          dataIndex: 'ANSWER_GROUP_DESCRIPT',
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '是否启用',
          dataIndex: 'IS_VALID',
          sorter: true,
          filterType: 'input',
          render: props => {
            return <div>{this.createDictText(props.row.IS_VALID, '1004')}</div>;
          }
        },
        {
          title: '数据来源',
          dataIndex: 'DATA_SOURCES',
          sorter: true,
          filterType: 'input',
          render: props => {
            return <div>{this.createDictText(props.row.DATA_SOURCES, '1045')}</div>;
          }
        },
        {
          title: '使用次数',
          dataIndex: 'NUMBER',
          sorter: true,
          filterType: 'input'
        }
      ];
    },
    createTopFilterList() {
      return [
        {
          type: 'INPUT',
          label: '答案名称',
          fieldName: 'answerGroupName',
          placeholder: '答案名称'
        },
        {
          type: 'SELECT',
          label: '是否启用',
          fieldName: 'isValid',
          placeholder: '是否启用',
          itemList: this.createDictList('1004')
        }
      ];
    },
    createAnswerTableColums() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          fixed: 'left',
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '选项内容',
          dataIndex: 'ANSWER',
          filterType: 'input'
        },
        {
          title: '排列顺序',
          dataIndex: 'ANSWER_SORT',
          filterType: 'input'
        },
        {
          title: '数据来源',
          dataIndex: 'DATA_SOURCES',
          filterType: 'input',
          render: props => {
            return <div>{this.createDictText(props.row.DATA_SOURCES, '1045')}</div>;
          }
        }
      ];
    },
    // 搜索
    changeHandle(val) {
      this.fetchParams = Object.assign({}, val, { xhrAbort: false });
      // 清空选项信息表格
      this.clearAnswerTable();
    },
    // 保存answerTable的数据，用于清空
    tableChangeFun(val) {
      this.tableData = val;
    },
    // 清空answerTable
    clearAnswerTable() {
      this.$nextTick(() => {
        this.AnswerTable.EXECUTE_DELETE(this.tableData);
      });
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    closeHandler(val) {
      this.visible = val;
    },
    // 根据答案组ID获取选项
    getAnswerByGroupId(row) {
      let searchDetail = {
        id: row.ANSWER_GROUP_ID
      };
      this.fetchAnswerParams = Object.assign(searchDetail, { xhrAbort: false });
    },
    // 取消
    goback() {
      this.$emit('fatherGoBack');
    },
    // 确定选择答案
    req() {
      if (this.chooseAnswer.length === 0) {
        this.$message.error('请选择一条答案');
        return;
      }
      let answerGroupId = this.chooseAnswer[0].ANSWER_GROUP_ID;
      let answerGroupName = this.chooseAnswer[0].ANSWER_GROUP_NAME;
      getAnswerStringById(answerGroupId).then(res => {
        let val = {
          answerGroupId: answerGroupId,
          answerGroupName: answerGroupName,
          answer: res.data
        };
        this.$emit('fatherReq', val);
      });
    }
  }
};
</script>

<style scoped>
.dialog-form {
  width: 100%;
  margin: 0;
}

.el-checkbox-group .el-checkbox + .el-checkbox {
  margin: 0;
}
</style>
